/*  Primer, v2 - Prime finder.
 *
 *	Streamlined version of v1.
 *  Basically only brought over the checkprime() function,
 *  and wrote a new main().
 *
 *  ~Xevious 2-9-09 (later that day...)
 */
#include <cstdlib>
#include <iostream>
#include <math.h>

using namespace std;

bool checkprime(unsigned long number);

int main()
{
    int iterations = 0;  // number of primes to find. [ user defined ]
    int x = 0; // number of primes found.
    unsigned long prime = 3; // variable to store prime for checking.
    cin >> iterations;
    
    while (x <= iterations) // when primes found is less then primes wanted...
    {
          if (checkprime(prime))
          {
              cout << " " << prime;
              ++x;
          }
          
          prime += 2;
    }
    cout << "\n";
    system("PAUSE");
    return EXIT_SUCCESS;
}

bool checkprime(unsigned long number) // this function will test if a number is a prime number 
									  // or not. Boolean.
{
    if (number < 2) // 0 and 1 apparently aren't primes, and this also keeps out negatives.
    {
        return false;
    }
    
    else if (number % 2 == 0) // if number div 2, then it's even.
    {
        return false;
    }
    
    else
    {
        int factorhigh = int(sqrt(number)+1); // highest number to check against.
        int divisor = 3; // start dividing by 3.
        
        while (divisor <= factorhigh) // check for division.
        {
              if (number % divisor == 0)
              {
                         return false;
              }
              divisor += 2;
        }
        
        return true; // well if it got here then it must be a prime.
   }
}
